<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理后台 - 角色管理</title>
    <link href="{{asset('/user/css/bootstrap.min.css?v=3.3.5')}}" rel="stylesheet">
    <link href="{{asset('/user/css/animate.min.css')}}" rel="stylesheet">
    <link href="{{asset('/user/css/total.css')}}" rel="stylesheet">
    <link href="{{asset('/user/css/btn.css')}}" rel="stylesheet">
    <style>
        .btn-default {width: 120px; height: 30px; line-height: 30px; display: inline-block; border: 1px solid #dee5e7; text-align: center; border-radius: 3px; cursor: pointer; background-color: #fff; color: #000;}
        .two{margin-left: 30px; float: left;}
        .one-name,.two-name{margin-left: 3px; font-size: 16px;}
        .add{width: 13px; height: 13px; float: left; margin-top: 5px; background: url("{{asset('/user/img/plus.gif')}}") no-repeat;}
        .minus{width: 13px; height: 13px; float: left; margin-top: 5px; background: url("{{asset('/user/img/minus.gif')}}") no-repeat;}
        .float-e-margins .btn{bottom: 30px; right: 30px;}
        .ibox-content{position: relative;}
        .theader_th{background: #2F4050 !important; opacity: 0.7; color: #fff; text-align: center;}
        tbody>tr>td{text-align: center;}
        .checkbox{float: left;}
        .gohome{display: none;}
        .ant-btn-primary{background-color:#3475c3!important; border-color: #3475c3;}
    </style>
</head>
<body>

<!-- 分配权限 -->
<div id="jurisdiction_box" class="ant-modal" style="width: 520px; transform-origin: 1054px 10px 0px; width: 100%; height: 100%; top: 0">
    <div class="ant-modal-content" style="height: 100%">
        <button  class="ant-modal-close"  onclick="CloseDiv('jurisdiction_box','mask')">
        </button>
        <div class="ant-modal-header"><div class="ant-modal-title"><span class="power"></span>修改密钥权限</div></div>
        <div class="ant-modal-body">
            <form class="ant-form ant-form-horizontal">
                <div class="ant-row ant-form-item">
                    <div id="ibox-content"  class="" style="padding-left: 30px;">
                        <div class="box" style="display: none;">
                            <div class="one">
                                <input type="checkbox" name="" id="" value="" style="display: none"/>
                                <div class="add"></div>
                                <span class="one-name"></span>
                            </div>
                        </div>
                        <div class="two" style="display: none;">
                            <input type="checkbox"  name="" class="checkbox" value=""/>
                            <span class="two-name" data-id=' ' ></span>
                        </div>
                    </div>
                    <div class="two" style="display: none;">
                        <input type="checkbox"  name="" class="checkbox" value=""/>
                        <span class="two-name" data-id=' ' ></span>
                    </div>
                </div>
                <div class="ant-row ant-form-item modal-btn form-button" style="margin-top: 24px; text-align: center;">
                    <div class="ant-col-22 ant-form-item-control-wrapper">
                        <div class="ant-form-item-control "><button type="button" class="ant-btn ant-btn-primary ant-btn-lg" id="sure_submit"><span>确定修改</span></button></div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<input type="hidden" class="js_role_id">
<script src="{{asset('/user/js/jquery.min.js?v=2.1.4')}}"></script>
<script src="{{asset('/layuiadmin/layui/layui.js')}}"></script>
<script src="{{asset('/layuiadmin/jquery.min.js')}}"></script>
<script src="{{asset('/layuiadmin/jquerysession.js')}}"></script>
<script>
    var token = localStorage.getItem("Usertoken");
    var str=location.search;
    var pass_id=str.split('?')[1];
    
    layui.config({
          base: '../../layuiadmin/' //静态资源所在路径
      }).extend({
          index: 'lib/index' //主入口模块
      }).use(['index','form','table','laydate'], function(){
          
        $(document).ready(function () {
            render();
            function render() {
                var jsonDataArr = "";
    
                var role_id=$(this).attr("data-id");
                var name=$(this).attr('data-name');
                $('.power').html(name);
                $('.js_role_id').val(role_id);
                
                var pass = $.session.get('pass');//获取有无设置过期时间cookie
                //权限层级列表
                $.ajax({
                    type: "post",
                    url:  location.protocol+'//'+document.domain+"/api/glow/pass_find",
                    async: true,
                    dataType: 'json',
                    data: {
                        token: token,
                        id: pass_id,
                        path: 'api/glow/pass_find',
                        pass: pass
                    },
                    success: function (res) {
                        if(res.status == '2'){
                            layer.msg(res.message, {
                                offset: '50px'
                                ,icon: 2
                                ,time: 3000
                            });
                        }else{
                            jsonDataArr = res.data;
                            //定义2个数组,一个存放一级目录,一个为二级目录
                            var oneArr = [];
                            var twoArr = [];
                            var html = "";
                            for(var i = 0; i < jsonDataArr.length; i++){
                                if(jsonDataArr[i].status == 1){
                                    html+='<div class="box" style="display: block;">';
                                    html+='<div class="one" data-id="'+jsonDataArr[i].id+'">';
                                    html+='<input type="checkbox" name="" id="" value="" style="display: none"/>';
                                    html+='</div>';
                                    html+='<div class="two" style="display: block;" data-id="'+jsonDataArr[i].id+'">';
                                    html+='<input type="checkbox"  name="" class="checkbox" value="" checked/>';
                                    html+='<span class="two-name" data-id="">'+jsonDataArr[i].permission_name+'</span>';
                                    html+='</div>';
                                    html+='</div>';
                                }else{
                                    html+='<div class="box" style="display: block;">';
                                    html+='<div class="one" data-id="'+jsonDataArr[i].id+'">';
                                    html+='<input type="checkbox" name="" id="" value="" style="display: none"/>';
                                    html+='</div>';
                                    html+='<div class="two" style="display: block;" data-id="'+jsonDataArr[i].id+'">';
                                    html+='<input type="checkbox"  name="" class="checkbox" value=""/>';
                                    html+='<span class="two-name" data-id="">'+jsonDataArr[i].permission_name+'</span>';
                                    html+='</div>';
                                    html+='</div>';
                                }
                            }
                            $('#ibox-content').append(html);
                            for(var m = 0; m < permission_idArr.length; m++){
                                $('#ibox-content .box .two').find('input').each(function(index, item){
                                    if( permission_idArr[m] == $(item).parent().attr('data-id') ){
                                        $(item).attr('checked', true);
                                    }
                                })
                            }
        
                            //角色权限分配
                            $('#sure_submit').click(function () {
                                
                                // 重置cookie有效期
                                var time = Date.parse(new Date()) + (5 * 60 * 1000);
                                $.session.set('time', time);
                                
                                var iDarr = [];
                                $(".box").find("input[type='checkbox']").each(function(i, e){
                                    if($(e).is(":checked")){
                                        iDarr.push($(e).parent().attr("data-id"))
                                        iDarr.join();
                                    }
                                });
                                var pass_ids=iDarr.join();
                                $.ajax({
                                    type: "post",
                                    url:  location.protocol+'//'+document.domain+"/api/glow/pass_edit", // role_permission/assign_permission
                                    async: true,
                                    dataType: 'json',
                                    data: {
                                        token: token,
                                        // phone: $("#phone").val(),
                                        permission_id: pass_ids,
                                        pass_id: pass_id,
                                        pass: pass,
                                    },
                                    success: function (res) {
                                        // console.log(res);
                                        if(res.status==1){
                                            layer.msg(res.message, {
                                                offset: '50px'
                                                ,icon: 1
                                                ,time: 2000
                                            });
                                        }else{
                                            layer.msg(res.message, {
                                                offset: '50px'
                                                ,icon: 2
                                                ,time: 3000
                                            });
                                        }
                                    },
                                });
                            });
                            //默认是收起来的 点击名称可以下拉
                            $("#ibox-content").on("click",".one",function(){
                                $(this).nextAll(".two").toggle();
                            });
                        }
                    },
                });
            }
        });

    //获取权限列表
    $('tbody').on("click","tr td .jurisdiction",function(){
        // console.log(2)
        ShowDiv('jurisdiction_box','mask')
    })
    var permission_idArr = [];

    //     //关闭弹出层
    function CloseDiv(show_div,bg_div){
        document.getElementById(show_div).style.display='none';
        document.getElementById(bg_div).style.display='none';
        window.location.reload()
    }

    $('#addrole_sure').click(function () {
        $(".input").each(function () {
            var val = $(this).val();
            if (val == "") {
                $(this).focus().css({
                    "border": "1px solid red"
                });
                $(this).next().show()
            }
        });
    });
          
    });
</script>
</body>
</html>